﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using myMvcBlog.Infrastructure;

namespace myMvcBlog
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.Add(
                    new Route("{XmlRpc}", null,
                        new RouteValueDictionary(new { XmlRpc = "XmlRpc" }),
                        new MetaWeblogRouteHandler()));

            routes.MapRoute(
                "PostsUnderCategory",
                "Admin/Categories/{categoryId}/Posts",
                new { controller = "Admin", action = "PostsByCategory" }
                );

            routes.MapRoute(
                "AdminDefault",
                "Admin",
                new { controller = "Admin", action = "Index" }
                );

            routes.MapRoute(
                "EditPost",
                "Admin/Edit/{postId}",
                new { controller = "Admin", action = "Edit" }
                );

            routes.MapRoute(
                "Archives",
                "Archives/{year}/{month}",
                new { controller = "Archive", action = "Index" }
                );

            routes.MapRoute(
                "ViewCategory",
                "Categories/{nicename}",
                new { controller = "Category", action = "View" }
                );

            routes.MapRoute(
                "ViewTag",
                "Tags/{nicename}",
                new { controller = "Tag", action = "View" }
                );
            routes.MapRoute(
                "DeletePostTag",
                "Tags/DeletePostTag/{postId}/{tagId}",
                new { controller = "Tag", action = "DeletePostTag" }
                );
            routes.MapRoute(
                "ViewPost",
                "posts/{nicename}",
                new { controller = "Home", action = "View" }
                );
            routes.MapRoute(
                "HomePager",
                "pages/{page}",
                new { controller = "Home", action = "HomePaging" }
                );
            routes.MapRoute(
                "Home",
                "",
                new { controller = "Home", action = "Index" }
                );
            routes.MapRoute(
                "API",
                "API/{action}",
                new { controller = "API" }
                );

            routes.MapRoute(
                "NotFound",
                "NotFound",
                new { controller = "Home", action = "NotFound" }
                );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            ModelBinders.Binders.Add(typeof(Post), new PostModelBinder());
            ModelBinders.Binders.Add(typeof(Comment), new CommentModelBinder());

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}